package com.xinrui.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.xinrui.sunnyday.constant.MessageConstant;
import com.xinrui.sunnyday.entity.PageResult;
import com.xinrui.sunnyday.entity.QueryPageBean;
import com.xinrui.sunnyday.entity.Result;
import com.xinrui.sunnyday.pojo.CheckGroup;
import com.xinrui.sunnyday.pojo.Setmeal;
import com.xinrui.sunnyday.service.SetMealService;
import com.xinrui.sunnyday.utils.QiNiuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Controller
@ResponseBody
@RequestMapping("/setMeal")
public class SetMealController {
    @Reference
    private SetMealService setMealService;

    /**
     * 分页查询
     *
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<Setmeal> setMealPageInfo = setMealService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(setMealPageInfo.getTotal(), setMealPageInfo.getList());
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, pageResult);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        setMealService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    /**
     * 查询检查组
     *
     * @return
     */
    @GetMapping("/findAllGroup")
    public Result findAllGroup() {
        List<CheckGroup> checkGroupList = setMealService.findAllGroup();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroupList);
    }

    /**
     * 文件上传
     *
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
//        1.获取图片名称截取后缀名
        String originalFilename = imgFile.getOriginalFilename();
//        后缀名
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        生成随机名称并拼接后缀名
        String fileName = UUID.randomUUID() + substring;

        try {
            //        调用七牛上传文件
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), fileName);
            //        返回数据给页面
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("imgName", fileName);
            hashMap.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, hashMap);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }

    }

    /**
     * 新增
     *
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setMealService.mealAndGroupAdd(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Setmeal setmeal = setMealService.findById(id);
//        使用一个map集合封装
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("setMeal", setmeal);
        hashMap.put("domain", QiNiuUtils.DOMAIN);
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, hashMap);
    }

    /**
     * 查询当前套餐的检查组
     *
     * @param id
     * @return
     */
    @GetMapping("/findByIdGroup")
    public Result findByIdGroup(Integer id) {
        Integer[] integers = setMealService.findByIdGroup(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, integers);
    }

    /**
     * 编辑当前套餐组
     *
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setMealService.update(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
}
